import { MiddlewareConsumer, Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Room } from "./entity/room.entity";
import { UserModule } from "./user/user.module";
import { RoomModule } from "./room/room.module";
import { MessageModule } from "./message/message.module";
import { User } from "./entity/user.entity";
import { EmailModule } from "./email/email.module";
import { CacheRedisModule } from "./cache-redis/cache-redis.module";
import { SongModule } from "./song/song.module";
import { Song } from "./entity/song.entity";
import { WsModule } from "./ws/ws.module";
import { UserMiddleware } from "./middleware/middleware";
import { HttpFilter } from "./http.filter";
import { APP_FILTER } from "@nestjs/core";
import { Message } from "./entity/message.entity";

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: "mysql",
      host: "192.168.137.20",
      port: 3306,
      username: "root",
      password: "123456",
      database: "data",
      synchronize: false,
      entities: [Room, User, Song, Message],
    }),
    UserModule,
    RoomModule,
    MessageModule,
    EmailModule,
    CacheRedisModule,
    SongModule,
    WsModule,
  ],
  providers: [
    {
      provide: APP_FILTER,
      useClass: HttpFilter,
    },
  ],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(UserMiddleware)
      .exclude(
        "/user/login",
        "/user/register",
        "/user/forget",
        "/email/sendEmailCode",
        "/public/(.*)"
      )
      .forRoutes("user", "email", "room", "song", "message");
  }
}
